using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;

/// <summary>
/// Summary description for CustomerAccess
/// </summary>
public class CustomerAccess
{
    public CustomerAccess()
    {

    }
    public static bool Restore_Password(string username, string pass)
    {
        DbCommand cmd = GenericDataAccess.CreateCommand();
        cmd.CommandText = "RestorePassword";

        DbParameter para = cmd.CreateParameter();
        para.ParameterName = "@username";
        para.Value = username;
        para.DbType = DbType.String;
        cmd.Parameters.Add(para);

        para = cmd.CreateParameter();
        para.ParameterName = "@pass";
        para.Value = pass;
        para.DbType = DbType.String;
        cmd.Parameters.Add(para);
        try
        {
            GenericDataAccess.ExecuteNoneQuery(cmd);
            return true;
        }
        catch
        {
            return false;
        }

    }
    public static DataTable GetCustomer(string username, string pass)
    {
        DbCommand command = GenericDataAccess.CreateCommand();
        command.CommandText = "GetCustomer";
        DbParameter param1 = command.CreateParameter();
        DbParameter param2 = command.CreateParameter();
        param1.ParameterName = "@username";
        param1.Value = username;
        param1.DbType = DbType.String;
        command.Parameters.Add(param1);
        param2.ParameterName = "@pass";
        param2.Value = pass;
        param2.DbType = DbType.String;
        command.Parameters.Add(param2);
        return GenericDataAccess.ExecuteSelectCommand(command);

    }
    public static bool AddCustomer(string name, string username, string pass, string sex, DateTime birth, string email, string phone, string province, string add)
    {
        DbCommand command = GenericDataAccess.CreateCommand();
        command.CommandText = "Insert_Customer";
        DbParameter para = command.CreateParameter();
        para.ParameterName = "@name";
        para.Value = name;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@username";
        para.Value = username;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@pass";
        para.Value = pass;
        para.DbType = DbType.String;
        command.Parameters.Add(para);


        para = command.CreateParameter();
        para.ParameterName = "@sex";
        para.Value = sex;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@birth";
        para.Value = birth;
        para.DbType = DbType.DateTime;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@email";
        para.Value = email;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@phone";
        para.Value = phone;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@province";
        para.Value = province;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@add";
        para.Value = add;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        try
        {
            GenericDataAccess.ExecuteNoneQuery(command);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public static bool ChangePassword(String username, string newpass, string oldpass)
    {
        DbCommand command = GenericDataAccess.CreateCommand();
        command.CommandText = "UpdatePassword_Customer";

        DbParameter para = command.CreateParameter();
        para.ParameterName = "@username";
        para.Value = username;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@passNew";
        para.Value = newpass;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@passOld";
        para.Value = oldpass;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        try
        {
            GenericDataAccess.ExecuteNoneQuery(command);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public static bool ChangeInformation(string username, string name, string sex, DateTime birthday, string email, string phone, string add, string province)
    {
        DbCommand command = GenericDataAccess.CreateCommand();
        command.CommandText = "Update_Customer";

        DbParameter para = command.CreateParameter();
        para.ParameterName = "@username";
        para.Value = username;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@name";
        para.Value = name;
        para.DbType = DbType.String;
        command.Parameters.Add(para);
        para = command.CreateParameter();
        para.ParameterName = "@sex";
        para.Value = sex;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@birth";
        para.Value = birthday;
        para.DbType = DbType.DateTime;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@email";
        para.Value = email;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@phone";
        para.Value = phone;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@add";
        para.Value = add;
        para.DbType = DbType.String;
        command.Parameters.Add(para);

        para = command.CreateParameter();
        para.ParameterName = "@province";
        para.Value = province;
        para.DbType = DbType.String;
        command.Parameters.Add(para);
        try
        {
            GenericDataAccess.ExecuteNoneQuery(command);
            return true;
        }

        catch
        {
            return false;
        }
    }
    public static bool UpdatePassword_Customer(string username, string pass)
    {
        DbCommand cmd = GenericDataAccess.CreateCommand();
        cmd.CommandText = "UpdatePassword_Customer";
        DbParameter para = cmd.CreateParameter();
        para.ParameterName = "@username";
        para.Value = username;
        para.DbType = DbType.String;
        cmd.Parameters.Add(para);
        para = cmd.CreateParameter();
        para.ParameterName = "@pass";
        para.Value = pass;
        para.DbType = DbType.String;
        cmd.Parameters.Add(para);
        try
        {
            GenericDataAccess.ExecuteNoneQuery(cmd);
            return true;
        }
        catch
        {
             return false;

        }
    }
    public static DataTable GetDetail_Customer(string username)
    {
        DbCommand cmd = GenericDataAccess.CreateCommand();
        cmd.CommandText = "GetDetail_Customer";
        DbParameter param = cmd.CreateParameter();
        param.ParameterName = "@username";
        param.Value = username;
        param.DbType = DbType.String;
        cmd.Parameters.Add(param);
        return GenericDataAccess.ExecuteSelectCommand(cmd);
    }
    public static void TuChoiThamGiaHeThong(string username, string pass)
    {
        DbCommand cmd = GenericDataAccess.CreateCommand();
        cmd.CommandText = "TuChoiThamGiaHeThong";

        DbParameter para = cmd.CreateParameter();
        para.ParameterName = "@username";
        para.Value = username;
        para.DbType = DbType.String;
        cmd.Parameters.Add(para);

        para = cmd.CreateParameter();
        para.ParameterName = "@pass";
        para.Value = pass;
        para.DbType = DbType.String;
        cmd.Parameters.Add(para);
        GenericDataAccess.ExecuteNoneQuery(cmd);
    }
}